package com.test.daily.leetcode.y2022.m05.day0521.v04;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/5/21 下午 09:19
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {

    }

    public static class Node {
        int value;
        Node left;
        Node right;
        public Node(int value){
            this.value = value;
        }
    }

    public static void level(Node head) {
        if (head == null) {
            return;
        }
        Queue<Node> queue = new ArrayDeque<>();
        queue.add(head);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.println(" " + cur.value);
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }
    }

    public static Queue<String> preSerial(Node head) {
        Queue<String> ans = new LinkedList<>();
        pres(ans, head);
        return ans;
    }

    public static void pres(Queue ans, Node head) {
        if (head == null) {
            ans.add(null);
        } else {
            ans.add(Integer.toString(head.value));
            pres(ans, head.left);
            pres(ans, head.right);
        }
    }

    public static Queue<String> inSerial(Node head) {
        Queue<String> ans = new LinkedList<>();
        in(head, ans);
        return ans;
    }

    public static void in(Node root, Queue<String> ans) {
        if (root == null) {
            ans.add(null);
        } else {
            in(root.left, ans);
            ans.add(Integer.toString(root.value));
            in(root.right, ans);
        }
    }
    public static void postSerial(Node root){
        Queue<String> ans = new LinkedList<>();
        post(root, ans);
    }
    public static void post(Node root, Queue<String> ans){
        if(root == null){
            return;
        } else {
            post(root.left, ans);
            post(root.right, ans);
            ans.add(Integer.toString(root.value));
        }
    }
    public static Node dePreSerial(Queue<String> queue){
        if(queue.isEmpty() || queue == null){
            return null;
        }
        Node ans = dePre(queue);
        return ans;
    }
    public static Node dePre(Queue<String> queue){
        String value = queue.poll();
        if(value == null){
            return null;
        }
        Node head = new Node(Integer.valueOf(value));
        head.left = dePre(queue);
        head.right = dePre(queue);
        return head;
    }
}
